//
//  GLView.m
//  V3d_Cocoa
//
//  Created by Y my on 11-3-10.
//  Copyright 2011 NJUST. All rights reserved.
//

#import "GLView.h"

@implementation GLView

// init & dealloc //////////////////////////////////

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
		distanceToAngle = 0.0628f;
    }
    return self;
}

- (void)awakeFromNib {
	if (!renderer)
	{
		CGSize size = [self bounds].size;
		RGB black;
		SET_COLOR(black, 0, 0, 0);
		renderer = [[MIPRenderer alloc] initWithViewWidth:size.width Height:size.height BackgroundColor:black];
	}	
}

- (void)dealloc {
	if (renderer) {
		[renderer release];
	}
	
	[super dealloc];
}

- (void)setRendererData:(id <VolDataDelegate> )volData
{
	if (renderer)
	{
		[renderer setVolData:volData ];
		[self updateView];
	}
}

- (void)setRendererViewWidth:(unsigned short)width
					  Height:(unsigned short)height
					BkgColor:(RGB)color
{
	if (renderer)
	{
		[renderer setViewWidth:width Height:height BackgroundColor:color];
		[self updateView];
	}
}

// draw ///////////////////////////////////

- (void)drawRect:(CGRect)dirtyRect {
	//NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
	CGContextRef theContext = UIGraphicsGetCurrentContext();
	CGContextSaveGState(theContext);
	
	[[UIColor cyanColor] set];
	//[NSBezierPath fillRect:dirtyRect];
	CGContextBeginPath(theContext);
	CGContextAddRect(theContext, [self bounds]);
	CGContextFillPath(theContext);

	CGContextDrawImage(theContext, [self bounds], pixmap);

	/*
	NSSize textSize = [@"This is glView" sizeWithAttributes:nil];
	CGRect r;
	r.origin.x = 1.0;
	r.origin.y = 1.0;
	r.size.height = textSize.height + 10;
	r.size.width = textSize.width + 10;
	CGContextBeginPath(theContext);
	CGContextAddEllipseInRect(theContext, r);
	CGContextFillPath(theContext);
	[@"This is glView" drawAtPoint:NSMakePoint(6, 6) withAttributes:nil];	
	 */
	
	CGContextRestoreGState(theContext);
}

// transform ///////////////////////////////////

- (void)reset
{
	[renderer reset];
	[self updateView];
}

// 下面两个函数中，X, Y：鼠标（手指）前进的方向，Distance：鼠标（手指）前进的距离
- (void)rotateByGestureVX:(float)vx VY:(float)vy Distance:(float)dis
{
	if (renderer)
	{
		float x, y;
		x = -vy;
		y = vx;
		float angle = dis * distanceToAngle;
		[renderer rotateX:x Y:y Angle:angle];
		[self updateView];
	}
}

- (void)translateByGestureVX:(float)vx VY:(float)vy Distance:(float)dis
{
	if (renderer)
	{
		[renderer translateX:dis*vx Y:dis*vy];
		[self updateView];
	}
}

// （X, Y）: 旋转轴上一点（另一点始终为原点，旋转轴始终位于xoy平面中），Angle：旋转角度（弧度，逆时针）
- (void)rotateX:(float)x Y:(float)y Angle:(float)angle
{
	if (renderer)
	{
		[renderer rotateX:x Y:y Angle:angle];
		[self updateView];
	}
}

// （X, Y）：平移向量
- (void)translateX:(float)x Y:(float)y
{
	if (renderer)
	{
		[renderer translateX:x Y:y];
		[self updateView];
	}
}

- (void)afterRotationWithInterfaceOrientation:(UIInterfaceOrientation)orientation
{
	CGFloat width;
	CGFloat	height;
	if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
	{
		width = 728;
		height = 736;
	}
	else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
	{
		width = 984;
		height = 480;
	}
	RGB rgb;
	rgb.r = 0;
	rgb.g = 0;
	rgb.b = 0;
	[renderer setViewWidth:width Height:height BackgroundColor:rgb];
	[self updateView];
}

////////////////// Observer /////////////////

- (void)updateView {
	if (!renderer) {
		return;
	}

	// get data and redraw
	if (pixmap)
	{
		CGImageRelease(pixmap);
	}
	pixmap = [renderer proceed];
	
	[self setNeedsDisplay];
	
}

@end